﻿// <copyright file="Program.cs" company="EDD Solutions Ltd.">
// Copyright (c) EDD Solutions Ltd. 2010.
//
// This file is part of Sharepoint.Manipulator.Net.Console.
//
// Sharepoint.Manipulator.Net.Console is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Sharepoint.Manipulator.Net.Console is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Sharepoint.Manipulator.Net.Console.  If not, see http://www.gnu.org/licenses/.
// </copyright>
// <author>$Author$</author>
// <date>$Date$</date>
// <revision>$Revision$</revision>
namespace Sharepoint.Manipulator.Net.Console
{
    using System;

    /// <summary>
    /// Class describing the command line program.
    /// </summary>
    public class Program
    {
        /// <summary>
        /// The operation to carry out.
        /// </summary>
        private IOperation operation;

        /// <summary>
        /// Initializes a new instance of the Program class.
        /// </summary>
        /// <param name="args">the program arguments</param>
        public Program(string[] args)
        {
            this.ParseOperation(args[0]);
            this.operation.ParseParameters(args);
        }

        /// <summary>
        /// Gets or sets the operation to carry out.
        /// </summary>
        public IOperation Operation
        {
            get { return this.operation; }
            set { this.operation = value; }
        }

        /// <summary>
        /// Entry point to the program
        /// </summary>
        /// <param name="args">the command line parameters</param>
        public static void Main(string[] args)
        {
            // simple version takes a minimum of 2 arguments
            // an operation and a location
            if (args.Length < 2)
            {
                Usage();
                return;
            }

            Program p = null;
            try
            {
                p = new Program(args);
                p.Run();
            }
            catch (Exception ex)
            {
                System.Console.WriteLine("Unable to run program.");
                System.Console.WriteLine(ex.Message);
                Usage();
                if (p != null && p.Operation != null)
                {
                    System.Console.WriteLine(p.operation.Usage());
                }

                System.Console.WriteLine(ex.StackTrace);
            }

            ////System.Console.WriteLine("Please hit return to exit the application.");
            ////System.Console.ReadLine();
        }

        /// <summary>
        /// Write out usage instructions.
        /// </summary>
        private static void Usage()
        {
            string usage = ">app.exe <operation> <location> <operation parameters>"
                + Environment.NewLine
                + "Avaliable operations:"
                + Environment.NewLine
                + "'exists'"
                + Environment.NewLine
                + "'update'";
            System.Console.WriteLine(usage);
        }

        /// <summary>
        /// Run the required operation
        /// </summary>
        private void Run()
        {
            System.Console.WriteLine(this.operation.Run());
        }

        /// <summary>
        /// Interpret the location either as a URL or as a coded location.
        /// </summary>
        /// <param name="operation">the location string</param>
        private void ParseOperation(string operation)
        {
            switch (operation)
            {
                case "ex":
                case "exists":
                    this.operation = new Exists();
                    break;
                case "up":
                case "update":
                    this.operation = new Update();
                    break;
                default:
                    throw new Exception(string.Format("Unsupported operation [{0}]", new string[] { operation }));
            }
        }

        /// <summary>
        /// Interpret the task as coded string.
        /// </summary>
        /// <param name="task">the task string</param>
        private void ParseTask(string task)
        {
        }
    }
}
